<template>
  <div class="ten_box">
    <m-header modile-title="练习23" go-back="true"></m-header>
    <div class="main_box">
      <div style="background: #fffbe6">
        <div class="zt" v-for="(r,index) in list2" :key="index">
          <div class="zt-checkbox" v-if="r.flag_id && getFlagStart(r.flag_id,index).show">
            {{getFlagStart(r.flag_id,index).title}}</div>
          <div class="zt_title">{{index+1}}.{{r.title}}</div>
          <div class="zt_h">
            <div class="h_box" @click="choseMe(index,r2.rid,r2.name,index2,r.flag_id)" v-for="(r2,index2) in r.data" :key="index2">
              <div class="quan" :class="[checkIndex(index,index2,r.flag_id)?'active': '']"></div>
              <span>{{r2.name}}</span>
            </div>
          </div>
        </div>
      </div>
      <div class="button_div" @click="getValue">提交</div>
    </div>
  </div>
</template>

<script>
  import mHeader from '../../../../components/header'
  export default {
    data() {
      return {
        list2: [{
          title: '下雨天？',
          num: 1,
          data: [{
            name: '同意',
            rid: 101
          },
            {
              name: '反对',
              rid: 102
            },
            {
              name: '弃权',
              rid: 103
            },
            {
              name: '随多数',
              rid: 104
            }
          ]
        },
          {
            title: '对晴天的看法？',
            num: 2,
            data: [{
              name: '反对',
              rid: 201
            },
              {
                name: '同意',
                rid: 202
              },
              {
                name: '随多数',
                rid: 203
              },
              {
                name: '弃权',
                rid: 204
              }
            ]
          },
          {
            title: '对晴天的看法？',
            num: 3,
            flag_id: 12,
            data: [{
              name: '反对',
              rid: 201
            },
              {
                name: '同意',
                rid: 202
              },
              {
                name: '随多数',
                rid: 203
              },
              {
                name: '弃权',
                rid: 204
              }
            ]
          },
          {
            title: '对晴天的看法？',
            num: 4,
            flag_id: 12,
            data: [{
              name: '反对',
              rid: 201
            },
              {
                name: '同意',
                rid: 202
              },
              {
                name: '随多数',
                rid: 203
              },
              {
                name: '弃权',
                rid: 204
              }
            ]
          },
          {
            title: '对晴天的看法？',
            num: 4,
            flag_id: 12,
            data: [{
              name: '反对',
              rid: 201
            },
              {
                name: '同意',
                rid: 202
              },
              {
                name: '随多数',
                rid: 203
              },
              {
                name: '弃权',
                rid: 204
              }
            ]
          },
          {
            title: '对晴天的看法？',
            num: 3,
            flag_id: 13,
            data: [{
              name: '反对',
              rid: 201
            },
              {
                name: '同意',
                rid: 202
              },
              {
                name: '随多数',
                rid: 203
              },
              {
                name: '弃权',
                rid: 204
              }
            ]
          },
          {
            title: '对晴天的看法？',
            num: 4,
            flag_id: 13,
            data: [{
              name: '反对',
              rid: 201
            },
              {
                name: '同意',
                rid: 202
              },
              {
                name: '随多数',
                rid: 203
              },
              {
                name: '弃权',
                rid: 204
              }
            ]
          },
        ],
        selectIndex: '',
        selectArr: [],
        a: 0
      }
    },
    methods: {
      getFlagStart(id, index) {
        const hasIndex = this.list2.findIndex((item) => item.flag_id == id)

        let backIndex = [];

        const flagLen = this.list2.filter((item,key) => {
          if(item.flag_id == id){
            backIndex.push(key)
            return item
          }
        })
        return {
          show: hasIndex === index,
          title: `${flagLen.length}选1`,
          data: backIndex
        }
      },
      // 选中的样式
      checkIndex(index, index2) {
        const emptyArr = this.selectArr[index] || {}
        return emptyArr['index2'] === index2;
      },
      choseMe(index, id , name , index2, flag_id) {
        const selectVal = {
          id,
          name,
          index2
        }
        /* 如果是多选 */
        flag_id && this.choseCheckBox(flag_id,index,index2,selectVal)

        /* 单选用 */
        !flag_id && this.$set(this.selectArr, index, selectVal)

      },
      choseCheckBox(flag_id,index,index2,selectVal){
        selectVal.flag_id = flag_id;

        /* 当前多选的题目索引 */
        const indexArr = this.getFlagStart(flag_id).data;

        /* 随多数或者弃权 */
        if([2,3].includes(Number(index2))){
          // 这里是选择随多数或者弃权提示
          indexArr.map((item)=>{
            this.$set(this.selectArr, item, selectVal)
          })
          return false;
        }


        if([0,1].includes(Number(index2))){
          // 这里是选择随多数或者弃权提示
          indexArr.map((item)=>{
            // 清楚其他选中的2(随多数)、3（弃权）（这里注释掉就可以不用全部选择弃权或随多数，能选反对）
            if(this.selectArr[item] && [2,3].includes(Number(this.selectArr[item].index2))){
              this.$set(this.selectArr, item, {})
            }
            if(index2 === 1 && this.selectArr[item] && [1].includes(Number(this.selectArr[item].index2))){
              this.$set(this.selectArr, item, {})
            }
          })
        }


        this.$set(this.selectArr, index, selectVal);

      },
      getValue() {
        console.log(this.selectArr);
      }
    },
    components: {
      mHeader
    }
  }
</script>
<style lang="scss">
  .ten_box {
    padding-top: 60px;
    padding-bottom: 20%;
    .main_box {
      .zt {
        border-bottom: 10px solid #006699;
        padding: 20px;

        .zt-checkbox {
          font-size: 20px;
          font-weight: bold;
          color: #d86161;
          margin-bottom: 10px;
        }

        .zt_title {
          width: 100%;
          font-size: 24px;
        }

        .zt_h {
          display: flex;
          width: 100%;
          padding: 20px 10px;

          .h_box {
            display: inline-flex;
            align-items: center;
            width: 25%;

            .quan {
              width: 40px;
              height: 40px;
              border: 4px solid #d86161;
              border-radius: 50%;
            }

            .active {
              position: relative;
              display: inline-block;
              width: 40px;
              height: 40px;
              background: #d86161;
              border-radius: 25px;
            }

            .active::after {
              content: "";
              position: absolute;
              left: 7px;
              top: 8px;
              width: 60%;
              height: 35%;
              border: 5px solid #fffbe6;
              border-radius: 1px;
              border-top: none;
              border-right: none;
              background: transparent;
              transform: rotate(-45deg);
            }

            span {
              font-size: 25px;
              margin-left: 10px;
            }
          }
        }
      }

      .button_div {
        position: fixed;
        width: 100%;
        height: 60px;
        background: #d86161;
        color: white;
        font-size: 25px;
        bottom: 0;
        text-align: center;
        line-height: 60px;
      }

      .button_div:after {
        content: '';
        display: block;
        width: 100%;
        height: 100%;
        pointer-events: none;
        background-image: radial-gradient(circle, #fff 10%, transparent 10.01%);
        background-repeat: no-repeat;
        background-position: 50%;
        transform: scale(10, 10);
        opacity: 0;
        transition: transform .3s, opacity .5s;
      }

      .button_div:active:after {
        transform: scale(0, 0);
        opacity: .3;
        transition: 0s;
      }
    }
  }
</style>
